package com.villaday.idf.activity;

import android.content.Intent;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter.DateCheckInAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model.HousesStates;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ChouseCheckInDateActivity extends BaseActivity implements OkHttpUtil.OnDataListener, View.OnClickListener {


    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.rv_calendar)
    RecyclerView calendarRV;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();
    List<HousesStates.BodyEntity.MonthsEntity.ListEntity> dateCalendar = new ArrayList<>();
    private String houseId;
    private DateCheckInAdapter dateCheckInAdapter;

    private int startPosition = -1;
    private int endPosition = -1;


    @Override
    protected int getViewResId() {
        return R.layout.activity_chouse_check_in_date;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "选择日期", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 7);
        mGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {

                return dateCalendar.get(position).getMyType() == 1 ? 1 : 7;
            }
        });
        calendarRV.setLayoutManager(mGridLayoutManager);
        dateCheckInAdapter = new DateCheckInAdapter(this, this);

        calendarRV.setAdapter(dateCheckInAdapter);

    }

    @Override
    protected void loadData() {
        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getFragmentManager(), "loading");
        getCalendar();
    }

    private void getCalendar() {
        houseId = getIntent().getStringExtra("houseId");
        Map<String, String> datemap = new HashMap<>();

        datemap.clear();
        datemap.put("month", "");
        datemap.put("size", 6 + "");
        OkHttpUtil.getJSON(this, String.format(Constant.URL.houses_states, houseId), datemap, this);
    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (String.format(Constant.URL.houses_states, houseId).equals(url)) {
                LogeUtil.e("houses_states", json);
                dismissLoading();
                HousesStates housesStates = gson.fromJson(json, HousesStates.class);
                if ("200".equals(housesStates.getCode())) {
                    if (housesStates.getBody().getMonths() != null) {
                        HousesStates.BodyEntity.MonthsEntity.ListEntity listEntity = housesStates.getBody().getMonths().get(0).getList().get(0);

                    /*    if (listEntity.getDayOfWeek() != 7) {
                            for (int i = 0; i < listEntity.getDayOfWeek() - 1; i++) {
                                dateCalendar.add(new HousesStates.BodyEntity.MonthsEntity.ListEntity(1));
                            }
                        }*/


                        for (int i = 0; i < housesStates.getBody().getMonths().size(); i++) {
                          /*  for (int j = 0; j < 8 - housesStates.getBody().getMonths().get(i).getList().get(0).getDayOfWeek(); j++) {
                                housesStates.getBody().getMonths().get(i).getList().get(j).setFirstInGroup(true);
                            }
                            housesStates.getBody().getMonths().get(i).getList().get(0).setFirst(true);*/
                            dateCalendar.add(new HousesStates.BodyEntity.MonthsEntity.ListEntity(housesStates.getBody().getMonths().get(i).getList().get(0).getDate(), 1));

                            if (housesStates.getBody().getMonths().get(i).getList().get(0).getDayOfWeek() != 1) {
                                for (int j = 0; j < housesStates.getBody().getMonths().get(i).getList().get(0).getDayOfWeek() - 1; j++) {
                                    dateCalendar.add(new HousesStates.BodyEntity.MonthsEntity.ListEntity(-1));
                                }
                            }
                            dateCalendar.addAll(housesStates.getBody().getMonths().get(i).getList());

                        }

                        dateCheckInAdapter.setData(dateCalendar);
                    }
                } else {
                    ToastUtil.initToast(this, housesStates.getMsg());

                }
            }
        }
    }


    @Override
    public void onFailure(String url, String error) {
        dismissLoading();
    }


    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }

    }

    @Override
    public void onClick(View v) {
        int currentPosition = (int) v.getTag();

        if (startPosition == -1) {//没选择开始
            if (dateCalendar.get((Integer) v.getTag()).getStatus() != 1) {//点击售罄
                if (!TextUtils.isEmpty(dateCalendar.get((Integer) v.getTag()).getStatusText())) {
                    ToastUtil.initToast(this, dateCalendar.get((Integer) v.getTag()).getStatusText());

                }
            } else {
                startPosition = currentPosition;
                dateCalendar.get(currentPosition).setSelectType(1);
            }


        } else {//已选择开始
            if (currentPosition > startPosition) {
                for (int j = startPosition; j < currentPosition; j++) {
                    if (dateCalendar.get(j).getStatus() == 1 || dateCalendar.get(j).getStatus() == 0) {

                    } else {
                        ToastUtil.initToast(this, "日期包含" + dateCalendar.get(j).getStatusText());
                        return;
                    }
                }
            } else {
                for (int j = currentPosition; j < startPosition; j++) {
                    if (dateCalendar.get(j).getStatus() == 1 || dateCalendar.get(j).getStatus() == 0) {

                    } else {
                        ToastUtil.initToast(this, "日期包含" + dateCalendar.get(j).getStatusText());

                        return;
                    }
                }
            }
            if (currentPosition == startPosition && endPosition == -1) {
                dateCalendar.get(currentPosition).setSelectType(0);
                startPosition = -1;
                dateCheckInAdapter.notifyDataSetChanged();
                if (startPosition == -1 ) {
                    tv_confirm.setSelected(false);
                    tv_confirm.setText("请选择入店日期");
                }else if (  endPosition== -1){
                    tv_confirm.setSelected(false);
                    tv_confirm.setText("请选择离店日期");
                }
                else {
                    tv_confirm.setSelected(true);
                    tv_confirm.setText("确认");

                }
                return;

            } else if (currentPosition == endPosition || currentPosition == startPosition) {
                for (int i = startPosition; i < endPosition + 1; i++) {
                    dateCalendar.get(i).setSelectType(0);
                }
                startPosition = -1;
                endPosition = -1;
                dateCheckInAdapter.notifyDataSetChanged();
                if (startPosition == -1 ) {
                    tv_confirm.setSelected(false);
                    tv_confirm.setText("请选择入店日期");
                }else if (  endPosition== -1){
                    tv_confirm.setSelected(false);
                    tv_confirm.setText("请选择离店日期");
                }
                else {
                    tv_confirm.setSelected(true);
                    tv_confirm.setText("确认");

                }
                return;
            }


            if (endPosition == -1) {//没选择结束
                if (currentPosition > startPosition) {//向后选择
                    // 设置选中
                    for (int i = startPosition + 1; i < currentPosition; i++) {
                        dateCalendar.get(i).setSelectType(2);

                    }
                    dateCalendar.get(currentPosition).setSelectType(3);
                    dateCalendar.get(startPosition).setSelectType(4);

                    endPosition = currentPosition;

                } else if (currentPosition == startPosition) {//不处理

                } else {//向前选择
                    for (int i = startPosition -1; i > currentPosition; i--) {
                        dateCalendar.get(i).setSelectType(2);

                    }
                    dateCalendar.get(startPosition).setSelectType(3);
                    dateCalendar.get(currentPosition).setSelectType(4);
                    endPosition = startPosition;
                    startPosition = currentPosition;
                }
            } else {//已选择结束
                if (currentPosition > startPosition) {//向后选择
                    //选中的恢复
                    for (int i = startPosition + 1; i < endPosition; i++) {
                        dateCalendar.get(i).setSelectType(0);

                    }
                    dateCalendar.get(endPosition).setSelectType(0);

                    //设置选中
                    for (int i = startPosition + 1; i < currentPosition; i++) {
                        dateCalendar.get(i).setSelectType(2);

                    }
                    dateCalendar.get(currentPosition).setSelectType(3);
                    dateCalendar.get(startPosition).setSelectType(4);

                    endPosition = currentPosition;

                } else if (currentPosition == startPosition) {//不处理

                } else {//向前选择
                    //选中的恢复
                    for (int i = startPosition + 1; i < endPosition; i++) {
                        dateCalendar.get(i).setSelectType(0);

                    }
                    dateCalendar.get(endPosition).setSelectType(0);

                    //设置选中
                    dateCalendar.get(currentPosition).setSelectType(4);
                    dateCalendar.get(startPosition).setSelectType(3);
                    endPosition = startPosition;
                    startPosition = currentPosition;

                    for (int i = startPosition + 1; i < endPosition; i++) {
                        dateCalendar.get(i).setSelectType(2);

                    }

                    //endPosition = currentPosition;


                }
            }

        }


        dateCheckInAdapter.notifyDataSetChanged();
        if (startPosition == -1 ) {
            tv_confirm.setSelected(false);
            tv_confirm.setText("请选择入店日期");
        }else if (  endPosition== -1){
            tv_confirm.setSelected(false);
            tv_confirm.setText("请选择离店日期");
        }
        else {
            tv_confirm.setSelected(true);
            tv_confirm.setText("确认");

        }
    }


    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        if (endPosition == -1 || startPosition == -1) {
            ToastUtil.initToast(this, "请选择日期区间");
        } else {
            if (getIntent().getBooleanExtra("isNextChoose",false)){
                Intent intent = new Intent();
                intent.putExtra("checkInDate",dateCalendar.get(startPosition).getDate());
                intent.putExtra("checkOutDate",dateCalendar.get(endPosition).getDate());

                setResult(RESULT_OK,intent);
            }else {
                Intent intent3 = new Intent(this, CreatOrderActivity.class);
                intent3.putExtra("nightNum", endPosition-startPosition);


                intent3.putExtra("type", getIntent().getIntExtra("type", 0));
                intent3.putExtra("product_id", getIntent().getStringExtra("product_id"));
                intent3.putExtra("product_type", getIntent().getIntExtra("product_type", 100));
                intent3.putExtra("product_quantity", getIntent().getIntExtra("product_quantity", 1));
                intent3.putExtra("checkInDate",dateCalendar.get(startPosition).getDate());
                intent3.putExtra("checkOutDate",dateCalendar.get(endPosition).getDate());

                intent3.putExtra("houseId", getIntent().getStringExtra("houseId"));

                startActivity(intent3);
            }

            onBackPressed();
        }
    }
}
